Fix ctrl-c when debugging WOW64 processes
authorHannes Domani <ssbssa@yahoo.de>
Thu, 17 Sep 2020 17:10:16 +0000 (19:10 +0200)
committerHannes Domani <ssbssa@yahoo.de>
Thu, 17 Sep 2020 20:17:01 +0000 (22:17 +0200)
DebugBreakProcess starts a new thread in the target process with the
entry point DbgUiRemoteBreakin, where an int3 triggers a breakpoint
exception for gdb.

But this uses DbgUiRemoteBreakin of the 64bit ntdll.dll even for
WOW64 processes.
It stops in 64bit code, Wow64GetThreadContext reports a wrong pc without
the int3, and gdb lets the target process continue.

So this uses DbgUiRemoteBreakin of the 32bit ntdll.dll as the thread
entry point for WOW64 processes instead.

gdb/ChangeLog:

2020-09-17  Hannes Domani  <ssbssa@yahoo.de>

* windows-nat.c (ctrl_c_handler): Use 32bit DbgUiRemoteBreakin
for WOW64 processes.

gdb/ChangeLog
gdb/windows-nat.c

index 3c8e4a1c5ea8cc2ff9b99bb021d851ca2bf1c7ce..4b54f4e52047d32b5ad38ba265e46c9788b093ff 100644 (file)
@@ -1,3 +1,8 @@
+2020-09-17  Hannes Domani  <ssbssa@yahoo.de>
+
+       * windows-nat.c (ctrl_c_handler): Use 32bit DbgUiRemoteBreakin
+       for WOW64 processes.
+
 2020-09-17  Tom Tromey  <tom@tromey.com>
 
        * dwarf2/read.c (compute_compunit_symtab_includes): Use htab_up.
index 188a920cbb061f0d8639340ffec40b7d9afd4b3b..ec5e4281269e7f1ea8bf12cc62cd380358da0104 100644 (file)
@@ -71,6 +71,7 @@
 #include "gdbsupport/pathstuff.h"
 #include "gdbsupport/gdb_wait.h"
 #include "nat/windows-nat.h"
+#include "gdbsupport/symbol.h"
 
 using namespace windows_nat;
 
@@ -235,6 +236,7 @@ static int saw_create;
 static int open_process_used = 0;
 #ifdef __x86_64__
 static bool wow64_process = false;
+static void *wow64_dbgbreak;
 #endif
 
 /* User options.  */
@@ -1522,9 +1524,36 @@ ctrl_c_handler (DWORD event_type)
   if (!new_console && !attach_flag)
     return TRUE;
 
-  if (!DebugBreakProcess (current_process_handle))
-    warning (_("Could not interrupt program.  "
-              "Press Ctrl-c in the program console."));
+#ifdef __x86_64__
+  if (wow64_process)
+    {
+      /* Call DbgUiRemoteBreakin of the 32bit ntdll.dll in the target process.
+        DebugBreakProcess would call the one of the 64bit ntdll.dll, which
+        can't be correctly handled by gdb.  */
+      if (wow64_dbgbreak == nullptr)
+       {
+         CORE_ADDR addr;
+         if (!find_minimal_symbol_address ("ntdll!DbgUiRemoteBreakin",
+                                           &addr, 0))
+           wow64_dbgbreak = (void *) addr;
+       }
+
+      if (wow64_dbgbreak != nullptr)
+       {
+         HANDLE thread = CreateRemoteThread (current_process_handle, NULL,
+                                             0, (LPTHREAD_START_ROUTINE)
+                                             wow64_dbgbreak, NULL, 0, NULL);
+         if (thread)
+           CloseHandle (thread);
+       }
+    }
+  else
+#endif
+    {
+      if (!DebugBreakProcess (current_process_handle))
+       warning (_("Could not interrupt program.  "
+                  "Press Ctrl-c in the program console."));
+    }
 
   /* Return true to tell that Ctrl-C has been handled.  */
   return TRUE;