From 70f08bdf0351995db15f0867d6e820c03ec66ce4 Mon Sep 17 00:00:00 2001
From: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
Date: Thu, 5 Mar 2009 14:01:14 +0100
Subject: [PATCH] inferior interruption improvements

- on windows, use the recommended way to interrupt a debugged process
- on unix, use SIGTRAP
- assorted minor improvements
---
 src/plugins/debugger/procinterrupt.cpp | 55 ++++++++------------------
 1 file changed, 16 insertions(+), 39 deletions(-)

diff --git a/src/plugins/debugger/procinterrupt.cpp b/src/plugins/debugger/procinterrupt.cpp
index 3eb38bc2c62..6af9e0e1a44 100644
--- a/src/plugins/debugger/procinterrupt.cpp
+++ b/src/plugins/debugger/procinterrupt.cpp
@@ -29,65 +29,42 @@
 
 #include "procinterrupt.h"
 
-#if defined(Q_OS_WIN)
-
-#include <windows.h>
-
 using namespace Debugger::Internal;
 
-typedef HANDLE (WINAPI *PtrCreateRemoteThread)(
-    HANDLE hProcess,
-    LPSECURITY_ATTRIBUTES lpThreadAttributes,
-    SIZE_T dwStackSize,
-    LPTHREAD_START_ROUTINE lpStartAddress,
-    LPVOID lpParameter,
-    DWORD dwCreationFlags,
-    LPDWORD lpThreadId);
+#if defined(Q_OS_WIN)
 
-PtrCreateRemoteThread resolveCreateRemoteThread()
-{
-    HINSTANCE hLib = LoadLibraryA("Kernel32");
-    return (PtrCreateRemoteThread)GetProcAddress(hLib, "CreateRemoteThread");
-}
+#define _WIN32_WINNT 0x0501 /* WinXP, needed for DebugBreakProcess() */
+
+#include <windows.h>
 
 bool Debugger::Internal::interruptProcess(int pID)
 {
-    DWORD pid = pID;
-    if (!pid)
+    if (pID <= 0)
         return false;
 
-    PtrCreateRemoteThread libFunc = resolveCreateRemoteThread();
-    if (libFunc) {
-        DWORD dwThreadId = 0;
-        HANDLE hproc = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
-        HANDLE hthread = libFunc(hproc, NULL, 0, (LPTHREAD_START_ROUTINE)DebugBreak, 0, 0, &dwThreadId);
-        CloseHandle(hthread);
-        if (dwThreadId)
-            return true;
-    }
-    
-    return false;
-}
+    HANDLE hproc = OpenProcess(PROCESS_ALL_ACCESS, false, pID);
+    if (hproc == NULL)
+        return false;
 
-#endif // defined(Q_OS_WIN)
+    bool ok = DebugBreakProcess(hproc) != 0;
 
+    CloseHandle(hproc);
 
+    return ok;
+}
 
-#if defined(Q_OS_LINUX) || defined(Q_OS_MAC)
+#else // Q_OS_WIN
 
 #include <sys/types.h>
 #include <signal.h>
 
-using namespace Debugger::Internal;
-
 bool Debugger::Internal::interruptProcess(int pID)
 {
-    int procId = pID;
-    if (procId != -1) {
-        if (kill(procId, SIGINT) == 0)
+    if (pID > 0) {
+        if (kill(pID, SIGTRAP) == 0)
             return true;
     }
     return false;
 }
 
-#endif // defined(Q_OS_LINUX) || defined(Q_OS_MAC)
+#endif // !Q_OS_WIN
-- 
GitLab