[llvm] r241271 - [Support] Lazy load of dbghlp.dll on Windows
Leny Kholodov
lkholodov at accesssoftek.com
Thu Jul 2 07:34:58 PDT 2015
Author: lkholodov
Date: Thu Jul 2 09:34:57 2015
New Revision: 241271
URL: http://llvm.org/viewvc/llvm-project?rev=241271&view=rev
Log:
[Support] Lazy load of dbghlp.dll on Windows
This patch changes linkage with dbghlp.dll for clang from static (at load time)
to on demand (at the first use of required functions). Clang uses dbghlp.dll
only in minor use-cases. First of all in case of crash and in case of plugin load.
The dbghlp.dll library can be absent on system. In this case clang will fail
to load. With lazy load of dbghlp.dll clang can work even if dbghlp.dll
is not available.
Differential Revision: http://reviews.llvm.org/D10737
Modified:
llvm/trunk/autoconf/configure.ac
llvm/trunk/cmake/config-ix.cmake
llvm/trunk/include/llvm/Config/config.h.cmake
llvm/trunk/include/llvm/Config/config.h.in
llvm/trunk/lib/Support/CMakeLists.txt
llvm/trunk/lib/Support/Windows/DynamicLibrary.inc
llvm/trunk/lib/Support/Windows/Signals.inc
llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp
Modified: llvm/trunk/autoconf/configure.ac
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/autoconf/configure.ac?rev=241271&r1=241270&r2=241271&view=diff
==============================================================================
--- llvm/trunk/autoconf/configure.ac (original)
+++ llvm/trunk/autoconf/configure.ac Thu Jul 2 09:34:57 2015
@@ -1628,7 +1628,6 @@ dnl===----------------------------------
AC_CHECK_LIB(m,sin)
if test "$llvm_cv_os_type" = "MingW" ; then
- AC_CHECK_LIB(imagehlp, main)
AC_CHECK_LIB(ole32, main)
AC_CHECK_LIB(psapi, main)
AC_CHECK_LIB(shell32, main)
Modified: llvm/trunk/cmake/config-ix.cmake
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/cmake/config-ix.cmake?rev=241271&r1=241270&r2=241271&view=diff
==============================================================================
--- llvm/trunk/cmake/config-ix.cmake (original)
+++ llvm/trunk/cmake/config-ix.cmake Thu Jul 2 09:34:57 2015
@@ -397,12 +397,10 @@ else ()
endif ()
if( MINGW )
- set(HAVE_LIBIMAGEHLP 1)
set(HAVE_LIBPSAPI 1)
set(HAVE_LIBSHELL32 1)
# TODO: Check existence of libraries.
# include(CheckLibraryExists)
- # CHECK_LIBRARY_EXISTS(imagehlp ??? . HAVE_LIBIMAGEHLP)
endif( MINGW )
if (NOT HAVE_STRTOLL)
Modified: llvm/trunk/include/llvm/Config/config.h.cmake
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Config/config.h.cmake?rev=241271&r1=241270&r2=241271&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Config/config.h.cmake (original)
+++ llvm/trunk/include/llvm/Config/config.h.cmake Thu Jul 2 09:34:57 2015
@@ -125,9 +125,6 @@
/* Define if you have the libdl library or equivalent. */
#cmakedefine HAVE_LIBDL ${HAVE_LIBDL}
-/* Define to 1 if you have the `imagehlp' library (-limagehlp). */
-#cmakedefine HAVE_LIBIMAGEHLP ${HAVE_LIBIMAGEHLP}
-
/* Define to 1 if you have the `m' library (-lm). */
#undef HAVE_LIBM
Modified: llvm/trunk/include/llvm/Config/config.h.in
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Config/config.h.in?rev=241271&r1=241270&r2=241271&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Config/config.h.in (original)
+++ llvm/trunk/include/llvm/Config/config.h.in Thu Jul 2 09:34:57 2015
@@ -137,9 +137,6 @@
/* Define if libedit is available on this platform. */
#undef HAVE_LIBEDIT
-/* Define to 1 if you have the `imagehlp' library (-limagehlp). */
-#undef HAVE_LIBIMAGEHLP
-
/* Define to 1 if you have the `m' library (-lm). */
#undef HAVE_LIBM
Modified: llvm/trunk/lib/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CMakeLists.txt?rev=241271&r1=241270&r2=241271&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CMakeLists.txt (original)
+++ llvm/trunk/lib/Support/CMakeLists.txt Thu Jul 2 09:34:57 2015
@@ -1,7 +1,7 @@
set(system_libs)
if( NOT MSVC )
if( MINGW )
- set(system_libs ${system_libs} imagehlp psapi shell32 ole32)
+ set(system_libs ${system_libs} psapi shell32 ole32)
elseif( CMAKE_HOST_UNIX )
if( HAVE_LIBRT )
set(system_libs ${system_libs} rt)
Modified: llvm/trunk/lib/Support/Windows/DynamicLibrary.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/DynamicLibrary.inc?rev=241271&r1=241270&r2=241271&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/DynamicLibrary.inc (original)
+++ llvm/trunk/lib/Support/Windows/DynamicLibrary.inc Thu Jul 2 09:34:57 2015
@@ -23,14 +23,6 @@
#include <ntverp.h>
#endif
-#ifdef __MINGW32__
- #if (HAVE_LIBIMAGEHLP != 1)
- #error "libimagehlp.a should be present"
- #endif
-#else
- #pragma comment(lib, "dbghelp.lib")
-#endif
-
namespace llvm {
using namespace sys;
@@ -39,10 +31,21 @@ using namespace sys;
//=== and must not be UNIX code.
//===----------------------------------------------------------------------===//
+typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
+static fpEnumerateLoadedModules fEnumerateLoadedModules;
static DenseSet<HMODULE> *OpenedHandles;
+static bool loadDebugHelp(void) {
+ HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
+ if (hLib) {
+ fEnumerateLoadedModules = (fpEnumerateLoadedModules)
+ ::GetProcAddress(hLib, "EnumerateLoadedModules64");
+ }
+ return fEnumerateLoadedModules != 0;
+}
+
static BOOL CALLBACK
-ELM_Callback(WIN32_ELMCB_PCSTR ModuleName, ULONG_PTR ModuleBase,
+ELM_Callback(WIN32_ELMCB_PCSTR ModuleName, DWORD64 ModuleBase,
ULONG ModuleSize, PVOID UserContext) {
OpenedHandles->insert((HMODULE)ModuleBase);
return TRUE;
@@ -57,7 +60,14 @@ DynamicLibrary DynamicLibrary::getPerman
if (OpenedHandles == 0)
OpenedHandles = new DenseSet<HMODULE>();
- EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
+ if (!fEnumerateLoadedModules) {
+ if (!loadDebugHelp()) {
+ assert(false && "These APIs should always be available");
+ return DynamicLibrary();
+ }
+ }
+
+ fEnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
// Dummy library that represents "search all handles".
// This is mostly to ensure that the return value still shows up as "valid".
return DynamicLibrary(&OpenedHandles);
Modified: llvm/trunk/lib/Support/Windows/Signals.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/Signals.inc?rev=241271&r1=241270&r2=241271&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/Signals.inc (original)
+++ llvm/trunk/lib/Support/Windows/Signals.inc Thu Jul 2 09:34:57 2015
@@ -31,10 +31,9 @@
#ifdef _MSC_VER
#pragma comment(lib, "psapi.lib")
- #pragma comment(lib, "dbghelp.lib")
#elif __MINGW32__
- #if ((HAVE_LIBIMAGEHLP != 1) || (HAVE_LIBPSAPI != 1))
- #error "libimagehlp.a & libpsapi.a should be present"
+ #if (HAVE_LIBPSAPI != 1)
+ #error "libpsapi.a should be present"
#endif
// The version of g++ that comes with MinGW does *not* properly understand
// the ll format specifier for printf. However, MinGW passes the format
@@ -103,6 +102,8 @@
DWORD64 Reserved[3];
KDHELP64 KdHelp;
} STACKFRAME64, *LPSTACKFRAME64;
+ #endif // !defined(__MINGW64_VERSION_MAJOR)
+#endif // __MINGW32__
typedef BOOL (__stdcall *PREAD_PROCESS_MEMORY_ROUTINE64)(HANDLE hProcess,
DWORD64 qwBaseAddress, PVOID lpBuffer, DWORD nSize,
@@ -122,40 +123,46 @@ typedef BOOL (WINAPI *fpStackWalk64)(DWO
PFUNCTION_TABLE_ACCESS_ROUTINE64,
PGET_MODULE_BASE_ROUTINE64,
PTRANSLATE_ADDRESS_ROUTINE64);
-static fpStackWalk64 StackWalk64;
+static fpStackWalk64 fStackWalk64;
typedef DWORD64 (WINAPI *fpSymGetModuleBase64)(HANDLE, DWORD64);
-static fpSymGetModuleBase64 SymGetModuleBase64;
+static fpSymGetModuleBase64 fSymGetModuleBase64;
typedef BOOL (WINAPI *fpSymGetSymFromAddr64)(HANDLE, DWORD64,
PDWORD64, PIMAGEHLP_SYMBOL64);
-static fpSymGetSymFromAddr64 SymGetSymFromAddr64;
+static fpSymGetSymFromAddr64 fSymGetSymFromAddr64;
typedef BOOL (WINAPI *fpSymGetLineFromAddr64)(HANDLE, DWORD64,
PDWORD, PIMAGEHLP_LINE64);
-static fpSymGetLineFromAddr64 SymGetLineFromAddr64;
+static fpSymGetLineFromAddr64 fSymGetLineFromAddr64;
typedef PVOID (WINAPI *fpSymFunctionTableAccess64)(HANDLE, DWORD64);
-static fpSymFunctionTableAccess64 SymFunctionTableAccess64;
+static fpSymFunctionTableAccess64 fSymFunctionTableAccess64;
+
+typedef DWORD (WINAPI *fpSymSetOptions)(DWORD);
+static fpSymSetOptions fSymSetOptions;
+
+typedef BOOL (WINAPI *fpSymInitialize)(HANDLE, PCSTR, BOOL);
+static fpSymInitialize fSymInitialize;
static bool load64BitDebugHelp(void) {
HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
if (hLib) {
- StackWalk64 = (fpStackWalk64)
+ fStackWalk64 = (fpStackWalk64)
::GetProcAddress(hLib, "StackWalk64");
- SymGetModuleBase64 = (fpSymGetModuleBase64)
+ fSymGetModuleBase64 = (fpSymGetModuleBase64)
::GetProcAddress(hLib, "SymGetModuleBase64");
- SymGetSymFromAddr64 = (fpSymGetSymFromAddr64)
+ fSymGetSymFromAddr64 = (fpSymGetSymFromAddr64)
::GetProcAddress(hLib, "SymGetSymFromAddr64");
- SymGetLineFromAddr64 = (fpSymGetLineFromAddr64)
+ fSymGetLineFromAddr64 = (fpSymGetLineFromAddr64)
::GetProcAddress(hLib, "SymGetLineFromAddr64");
- SymFunctionTableAccess64 = (fpSymFunctionTableAccess64)
+ fSymFunctionTableAccess64 = (fpSymFunctionTableAccess64)
::GetProcAddress(hLib, "SymFunctionTableAccess64");
+ fSymSetOptions = (fpSymSetOptions)::GetProcAddress(hLib, "SymSetOptions");
+ fSymInitialize = (fpSymInitialize)::GetProcAddress(hLib, "SymInitialize");
}
- return StackWalk64 != NULL;
+ return fStackWalk64 && fSymInitialize && fSymSetOptions;
}
- #endif // !defined(__MINGW64_VERSION_MAJOR)
-#endif // __MINGW32__
// Forward declare.
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
@@ -187,12 +194,12 @@ static void PrintStackTraceForThread(llv
#endif
// Initialize the symbol handler.
- SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES);
- SymInitialize(hProcess, NULL, TRUE);
+ fSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES);
+ fSymInitialize(hProcess, NULL, TRUE);
while (true) {
- if (!StackWalk64(machineType, hProcess, hThread, &StackFrame, Context, NULL,
- SymFunctionTableAccess64, SymGetModuleBase64, NULL)) {
+ if (!fStackWalk64(machineType, hProcess, hThread, &StackFrame, Context, 0,
+ fSymFunctionTableAccess64, fSymGetModuleBase64, 0)) {
break;
}
@@ -221,7 +228,7 @@ static void PrintStackTraceForThread(llv
static_cast<DWORD>(StackFrame.Params[3]));
#endif
// Verify the PC belongs to a module in this process.
- if (!SymGetModuleBase64(hProcess, PC)) {
+ if (!fSymGetModuleBase64(hProcess, PC)) {
OS << " <unknown module>\n";
continue;
}
@@ -234,7 +241,7 @@ static void PrintStackTraceForThread(llv
symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL64);
DWORD64 dwDisp;
- if (!SymGetSymFromAddr64(hProcess, PC, &dwDisp, symbol)) {
+ if (!fSymGetSymFromAddr64(hProcess, PC, &dwDisp, symbol)) {
OS << '\n';
continue;
}
@@ -250,7 +257,7 @@ static void PrintStackTraceForThread(llv
IMAGEHLP_LINE64 line = {};
DWORD dwLineDisp;
line.SizeOfStruct = sizeof(line);
- if (SymGetLineFromAddr64(hProcess, PC, &dwLineDisp, &line)) {
+ if (fSymGetLineFromAddr64(hProcess, PC, &dwLineDisp, &line)) {
OS << format(", %s, line %lu", line.FileName, line.LineNumber);
if (dwLineDisp > 0)
OS << format(" + 0x%lX byte(s)", dwLineDisp);
@@ -301,17 +308,13 @@ static void InitializeThreading() {
}
static void RegisterHandler() {
-#if __MINGW32__ && !defined(__MINGW64_VERSION_MAJOR)
- // On MinGW.org, we need to load up the symbols explicitly, because the
- // Win32 framework they include does not have support for the 64-bit
- // versions of the APIs we need. If we cannot load up the APIs (which
- // would be unexpected as they should exist on every version of Windows
- // we support), we will bail out since there would be nothing to report.
+ // If we cannot load up the APIs (which would be unexpected as they should
+ // exist on every version of Windows we support), we will bail out since
+ // there would be nothing to report.
if (!load64BitDebugHelp()) {
assert(false && "These APIs should always be available");
return;
}
-#endif
if (RegisteredUnhandledExceptionFilter) {
EnterCriticalSection(&CriticalSection);
Modified: llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp?rev=241271&r1=241270&r2=241271&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp (original)
+++ llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp Thu Jul 2 09:34:57 2015
@@ -33,6 +33,7 @@
#if defined(_MSC_VER)
#include <Windows.h>
#include <DbgHelp.h>
+#pragma comment(lib, "dbghelp.lib")
#endif
namespace llvm {
More information about the llvm-commits
mailing list