From 74d718c001a81dcd32a11aebc8806618aa235267 Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Mon, 5 Feb 2018 12:15:57 +0100 Subject: [PATCH] Fix MinGW valgrind build QueryFullProcessImageName is only available on Windows >= Vista. iSetting _WIN32_WINNT fixes availability on MinGW and moving the functionality to winutils avoids code duplication. Change-Id: I0ff1a12a1c092b1ad9cde75b636b52c5b959ce7d Reviewed-by: Orgad Shaneh Reviewed-by: David Schulz --- src/libs/utils/winutils.cpp | 21 +++++++++-- src/libs/utils/winutils.h | 3 ++ .../devicesupport/localprocesslist.cpp | 36 +------------------ src/plugins/valgrind/memchecktool.cpp | 12 ++----- 4 files changed, 26 insertions(+), 46 deletions(-) diff --git a/src/libs/utils/winutils.cpp b/src/libs/utils/winutils.cpp index d41cb693cda..140d94977d6 100644 --- a/src/libs/utils/winutils.cpp +++ b/src/libs/utils/winutils.cpp @@ -26,10 +26,10 @@ #include "winutils.h" #include "qtcassert.h" -// Enable WinAPI Windows XP and later +// Enable WinAPI Windows Vista and later #ifdef Q_OS_WIN #undef _WIN32_WINNT -#define _WIN32_WINNT 0x0501 +#define _WIN32_WINNT 0x0600 // Needed for QueryFullProcessImageName #include #endif @@ -168,6 +168,23 @@ QTCREATOR_UTILS_EXPORT bool is64BitWindowsBinary(const QString &binaryIn) #endif } +QTCREATOR_UTILS_EXPORT QString imageName(quint32 processId) +{ + QString result; +#ifdef Q_OS_WIN + HANDLE handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId); + if (handle == NULL) + return result; + + wchar_t path[MAX_PATH]; + DWORD pathLen = MAX_PATH; + if (QueryFullProcessImageName(handle, 0, path, &pathLen)) + result = QString::fromUtf16(reinterpret_cast(path)); + CloseHandle(handle); +#endif + return result; +} + WindowsCrashDialogBlocker::WindowsCrashDialogBlocker() #ifdef Q_OS_WIN : silenceErrorMode(SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS), diff --git a/src/libs/utils/winutils.h b/src/libs/utils/winutils.h index 6bb75f97d6e..314cce673d3 100644 --- a/src/libs/utils/winutils.h +++ b/src/libs/utils/winutils.h @@ -44,6 +44,9 @@ QTCREATOR_UTILS_EXPORT bool is64BitWindowsSystem(); // Check for a 64bit binary. QTCREATOR_UTILS_EXPORT bool is64BitWindowsBinary(const QString &binary); +// Get the path to the executable for a given PID. +QTCREATOR_UTILS_EXPORT QString imageName(quint32 processId); + // // RAII class to temporarily prevent windows crash messages from popping up using the // application-global (!) error mode. diff --git a/src/plugins/projectexplorer/devicesupport/localprocesslist.cpp b/src/plugins/projectexplorer/devicesupport/localprocesslist.cpp index 035a0457296..b0d95c64746 100644 --- a/src/plugins/projectexplorer/devicesupport/localprocesslist.cpp +++ b/src/plugins/projectexplorer/devicesupport/localprocesslist.cpp @@ -54,40 +54,6 @@ namespace Internal { #ifdef Q_OS_WIN -// Resolve QueryFullProcessImageNameW out of kernel32.dll due -// to incomplete MinGW import libs and it not being present -// on Windows XP. -static BOOL queryFullProcessImageName(HANDLE h, DWORD flags, LPWSTR buffer, DWORD *size) -{ - // Resolve required symbols from the kernel32.dll - typedef BOOL (WINAPI *QueryFullProcessImageNameWProtoType) - (HANDLE, DWORD, LPWSTR, PDWORD); - static QueryFullProcessImageNameWProtoType queryFullProcessImageNameW = 0; - if (!queryFullProcessImageNameW) { - QLibrary kernel32Lib(QLatin1String("kernel32.dll"), 0); - if (kernel32Lib.isLoaded() || kernel32Lib.load()) - queryFullProcessImageNameW = (QueryFullProcessImageNameWProtoType)kernel32Lib.resolve("QueryFullProcessImageNameW"); - } - if (!queryFullProcessImageNameW) - return FALSE; - // Read out process - return (*queryFullProcessImageNameW)(h, flags, buffer, size); -} - -static QString imageName(DWORD processId) -{ - QString rc; - HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION , FALSE, processId); - if (handle == INVALID_HANDLE_VALUE) - return rc; - WCHAR buffer[MAX_PATH]; - DWORD bufSize = MAX_PATH; - if (queryFullProcessImageName(handle, 0, buffer, &bufSize)) - rc = QString::fromUtf16(reinterpret_cast(buffer)); - CloseHandle(handle); - return rc; -} - LocalProcessList::LocalProcessList(const IDevice::ConstPtr &device, QObject *parent) : DeviceProcessList(device, parent) , m_myPid(GetCurrentProcessId()) @@ -108,7 +74,7 @@ QList LocalProcessList::getLocalProcesses() DeviceProcessItem p; p.pid = pe.th32ProcessID; // Image has the absolute path, but can fail. - const QString image = imageName(pe.th32ProcessID); + const QString image = Utils::imageName(pe.th32ProcessID); p.exe = p.cmdLine = image.isEmpty() ? QString::fromWCharArray(pe.szExeFile) : image; diff --git a/src/plugins/valgrind/memchecktool.cpp b/src/plugins/valgrind/memchecktool.cpp index d007f3580fb..db06b93f912 100644 --- a/src/plugins/valgrind/memchecktool.cpp +++ b/src/plugins/valgrind/memchecktool.cpp @@ -98,6 +98,8 @@ #include #include +#include + #include #endif @@ -1503,15 +1505,7 @@ void HeobData::processFinished() debugger->setStartMode(AttachExternal); debugger->setCloseMode(DetachAtClose); debugger->setContinueAfterAttach(true); - - HANDLE p = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, m_data[1]); - if (p != NULL) { - wchar_t path[MAX_PATH]; - DWORD pathLen = MAX_PATH; - if (QueryFullProcessImageName(p, 0, path, &pathLen)) - debugger->setInferiorExecutable(QString::fromWCharArray(path)); - CloseHandle(p); - } + debugger->setInferiorExecutable(Utils::imageName(m_data[1])); connect(m_runControl, &RunControl::started, this, &HeobData::debugStarted); connect(m_runControl, &RunControl::stopped, this, &HeobData::debugStopped);