Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
[Support] Lazy load of dbghlp.dll on Windows
Browse files Browse the repository at this point in the history
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



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241271 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Leny Kholodov committed Jul 2, 2015
1 parent 74f7b8a commit 7ee1913
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 50 deletions.
1 change: 0 additions & 1 deletion autoconf/configure.ac
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions cmake/config-ix.cmake
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions include/llvm/Config/config.h.cmake
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions include/llvm/Config/config.h.in
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/Support/CMakeLists.txt
@@ -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)
Expand Down
30 changes: 20 additions & 10 deletions lib/Support/Windows/DynamicLibrary.inc
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -57,7 +60,14 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
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);
Expand Down
63 changes: 33 additions & 30 deletions lib/Support/Windows/Signals.inc
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -122,40 +123,46 @@ typedef BOOL (WINAPI *fpStackWalk64)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64,
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);
Expand Down Expand Up @@ -187,12 +194,12 @@ static void PrintStackTraceForThread(llvm::raw_ostream &OS, HANDLE hProcess,
#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;
}

Expand Down Expand Up @@ -221,7 +228,7 @@ static void PrintStackTraceForThread(llvm::raw_ostream &OS, HANDLE hProcess,
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;
}
Expand All @@ -234,7 +241,7 @@ static void PrintStackTraceForThread(llvm::raw_ostream &OS, HANDLE hProcess,
symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL64);

DWORD64 dwDisp;
if (!SymGetSymFromAddr64(hProcess, PC, &dwDisp, symbol)) {
if (!fSymGetSymFromAddr64(hProcess, PC, &dwDisp, symbol)) {
OS << '\n';
continue;
}
Expand All @@ -250,7 +257,7 @@ static void PrintStackTraceForThread(llvm::raw_ostream &OS, HANDLE hProcess,
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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tools/llvm-symbolizer/LLVMSymbolize.cpp
Expand Up @@ -33,6 +33,7 @@
#if defined(_MSC_VER)
#include <Windows.h>
#include <DbgHelp.h>
#pragma comment(lib, "dbghelp.lib")
#endif

namespace llvm {
Expand Down

0 comments on commit 7ee1913

Please sign in to comment.