]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Cygwin: Fix putting inferior in foreground (fix input)
authorPedro Alves <pedro@palves.net>
Thu, 6 May 2021 19:59:14 +0000 (20:59 +0100)
committerPedro Alves <pedro@palves.net>
Tue, 27 Feb 2024 14:29:34 +0000 (14:29 +0000)
gdb.base/interrupt.exp reveals that inferior input is
broken on Cygwin:

  (gdb) continue
  Continuing.
  talk to me baby
  Input/output error                                   <<< BAD
  PASS: gdb.base/interrupt.exp: process is alive
  a
  [Thread 10688.0x2590 exited with code 1]
  [Thread 10688.0x248c exited with code 1]
  [Thread 10688.0x930 exited with code 1]
  [Thread 10688.0x2c98 exited with code 1]

  Program terminated with signal SIGHUP, Hangup.
  The program no longer exists.
  (gdb) FAIL: gdb.base/interrupt.exp: child process ate our char
  a
  Ambiguous command "a": actions, add-auto-load-safe-path, add-auto-load-scripts-directory, add-inferior...
  (gdb) ERROR: "" is not a unique command name.

The problem is that inflow.c:child_terminal_inferior is failing to put
the inferior in the foreground, because we're passing down the
inferior's Windows PID instead of the Cygwin PID to Cygwin tcsetpgrp.

That is fixed by this commit.  Afterwards we will get:

  (gdb) continue
  Continuing.
  talk to me baby
  PASS: gdb.base/interrupt.exp: process is alive
  a
  a                                                              <<< GOOD
  PASS: gdb.base/interrupt.exp: child process ate our char
  [New Thread 7236.0x1c58]

  Thread 6 received signal SIGINT, Interrupt.                    <<< new thread spawned for SIGINT
  [Switching to Thread 7236.0x1c58]
  0x00007ffa6643ea6b in TlsGetValue () from /cygdrive/c/Windows/System32/KERNELBASE.dll
  (gdb) FAIL: gdb.base/interrupt.exp: send_gdb control C

We still have the FAIL seen above because this change has another
consequence.  By failing to put the inferior in the foreground
correctly, Ctrl-C was always reaching GDB first.  Now that the
inferior is put in the foreground properly, Ctrl-C reaches the
inferior first, which results in a Windows Ctrl-C event, which results
in Windows injecting a new thread in the inferior to report the Ctrl-C
exception => SIGINT.  That is, running the test manually:

Before patch:

  (gdb) c
  Continuing.
  [New Thread 2352.0x1f5c]
  [New Thread 2352.0x1988]
  [New Thread 2352.0x18cc]
                                                           <<< Ctrl-C pressed.
  Thread 7 received signal SIGTRAP, Trace/breakpoint trap.
  [Switching to Thread 2352.0x18cc]
  0x00007ffa68930b11 in ntdll!DbgBreakPoint () from /cygdrive/c/Windows/SYSTEM32/ntdll.dll
  (gdb)

Above, GDB got the SIGINT, and it manually passes it down the
inferior, which reaches windows_nat_target::interrupt(), which
interrupts the inferior with DebugBreakProcess (which injects a new
thread in the inferior that executes an int3 instruction).

After this patch, we have (with "set debugexceptions on" so
DBG_CONTROL_C is visible):

  (gdb) c
  Continuing.
  [New Thread 9940.0x1168]
  [New Thread 9940.0x5f8]
  gdb: Target exception MS_VC_EXCEPTION at 0x7ffa6638cf19
  gdb: Target exception MS_VC_EXCEPTION at 0x7ffa6638cf19
  [New Thread 9940.0x3d8]
  gdb: Target exception DBG_CONTROL_C at 0x7ffa6643ea6b   <<< Ctrl-C

  Thread 7 received signal SIGINT, Interrupt.             <<< new injected thread
  [Switching to Thread 9940.0x3d8]
  0x00007ffa6643ea6b in TlsGetValue () from /cygdrive/c/Windows/System32/KERNELBASE.dll
  (gdb)

This new behavior is exactly the same as what you see with a MinGW GDB
build.  Also, SIGINT reaching inferior first is what you get on Linux
as well currently.

I wrote an initial fix for this before I discovered that Cygwin
downstream had a similar change, so I then combined the patches.  I am
adding a Co-Authored-By for that reason.

Co-Authored-By: Takashi Yano <takashi.yano@nifty.ne.jp>
Approved-By: Tom Tromey <tom@tromey.com>
Change-Id: I3a8c3355784c6a817dbd345ba9dac24be06c4b3f

gdb/inflow.c

index a413957a1dd9bc5591353de3d2f5a4eee3042a60..3dd70b97fe53a5d298aca5b5a6c8efb57705114a 100644 (file)
 #include <sys/ioctl.h>
 #endif
 
+#ifdef __CYGWIN__
+#include <sys/cygwin.h>
+#endif
+
 #ifndef O_NOCTTY
 #define O_NOCTTY 0
 #endif
@@ -345,11 +349,25 @@ child_terminal_inferior (struct target_ops *self)
             then restore whatever was the foreground pgrp the last
             time the inferior was running.  See also comments
             describing terminal_state::process_group.  */
-#ifdef HAVE_GETPGID
-         result = tcsetpgrp (0, getpgid (inf->pid));
-#else
-         result = tcsetpgrp (0, tinfo->process_group);
+         pid_t pgrp = tinfo->process_group;
+#ifdef __CYGWIN__
+         /* The Windows native target uses Win32 routines to run or
+            attach to processes (CreateProcess / DebugActiveProcess),
+            so a Cygwin inferior has a Windows PID, rather than a
+            Cygwin PID.  We want to pass the Cygwin PID to Cygwin
+            tcsetpgrp if we have a Cygwin inferior, so try to convert
+            first.  If we have a non-Cygwin inferior, we'll end up
+            passing down the WINPID to tcsetpgrp, stored in
+            terminal_state::process_group.  tcsetpgrp still succeeds
+            in that case, and it seems preferable to switch the
+            foreground pgrp away from GDB, for consistency.  */
+         pid_t cygpid = cygwin_internal (CW_WINPID_TO_CYGWIN_PID, inf->pid);
+         if (cygpid <= cygwin_internal (CW_MAX_CYGWIN_PID))
+           pgrp = getpgid (cygpid);
+#elif defined (HAVE_GETPGID)
+         pgrp = getpgid (inf->pid);
 #endif
+         result = tcsetpgrp (0, pgrp);
          if (result == -1)
            {
 #if 0