public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Hannes Domani <ssbssa@yahoo.de>
To: gdb-patches@sourceware.org
Subject: [PATCH] [RFC] Move SetConsoleCtrlHandler calls to async thread
Date: Mon, 28 Nov 2022 20:15:26 +0100	[thread overview]
Message-ID: <20221128191526.1426-1-ssbssa@yahoo.de> (raw)
In-Reply-To: <20221128191526.1426-1-ssbssa.ref@yahoo.de>

Ctrl-C and Ctrl-Break don't work any more since the async commit, so I've tried
to move the SetConsoleCtrlHandler calls.
Now Ctrl-C seems to work, but Ctrl-Break completely stop both gdb and the
inferior (if new-console is disabled).

I'm not sure what's the proper fix is here, please advise.
---
 gdb/windows-nat.c | 66 ++++++++++++++++++++++++++---------------------
 1 file changed, 37 insertions(+), 29 deletions(-)

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 17422e15f80..e1034e48da3 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -209,6 +209,8 @@ static CORE_ADDR cygwin_get_dr (int i);
 static unsigned long cygwin_get_dr6 (void);
 static unsigned long cygwin_get_dr7 (void);
 
+static BOOL WINAPI ctrl_c_handler (DWORD event_type);
+
 /* User options.  */
 static bool new_console = false;
 #ifdef __CYGWIN__
@@ -477,7 +479,37 @@ windows_nat_target::process_thread ()
 	{
 	  if (!m_debug_event_pending)
 	    {
+	      /* If the user presses Ctrl-c while the debugger is waiting
+		 for an event, he expects the debugger to interrupt his
+		 program and to get the prompt back.  There are two
+		 possible situations:
+
+		   - The debugger and the program do not share the console, in
+		     which case the Ctrl-c event only reached the debugger.
+		     In that case, the ctrl_c handler will take care of
+		     interrupting the inferior.  Note that this case is
+		     working starting with Windows XP.  For Windows 2000,
+		     Ctrl-C should be pressed in the inferior console.
+
+		   - The debugger and the program share the same console, in
+		     which case both debugger and inferior will receive the
+		     Ctrl-c event.  In that case the ctrl_c handler will
+		     ignore the event, as the Ctrl-c event generated inside
+		     the inferior will trigger the expected debug event.
+
+		     FIXME: brobecker/2008-05-20: If the inferior receives the
+		     signal first and the delay until GDB receives that signal
+		     is sufficiently long, GDB can sometimes receive the SIGINT
+		     after we have unblocked the CTRL+C handler.  This would
+		     lead to the debugger stopping prematurely while handling
+		     the new-thread event that comes with the handling of the
+		     SIGINT inside the inferior, and then stop again
+		     immediately when the user tries to resume the execution
+		     in the inferior.  This is a classic race that we should
+		     try to fix one day.  */
+	      SetConsoleCtrlHandler (&ctrl_c_handler, TRUE);
 	      wait_for_debug_event (&m_last_debug_event, INFINITE);
+	      SetConsoleCtrlHandler (&ctrl_c_handler, FALSE);
 	      m_debug_event_pending = true;
 	    }
 	  serial_event_set (m_wait_event);
@@ -505,7 +537,11 @@ windows_nat_target::wait_for_debug_event_main_thread (DEBUG_EVENT *event)
 	  serial_event_clear (m_wait_event);
 	}
       else
-	wait_for_debug_event (event, INFINITE);
+	{
+	  SetConsoleCtrlHandler (&ctrl_c_handler, TRUE);
+	  wait_for_debug_event (event, INFINITE);
+	  SetConsoleCtrlHandler (&ctrl_c_handler, FALSE);
+	}
       return false;
     });
 }
@@ -1844,35 +1880,7 @@ windows_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
 
   while (1)
     {
-      /* If the user presses Ctrl-c while the debugger is waiting
-	 for an event, he expects the debugger to interrupt his program
-	 and to get the prompt back.  There are two possible situations:
-
-	   - The debugger and the program do not share the console, in
-	     which case the Ctrl-c event only reached the debugger.
-	     In that case, the ctrl_c handler will take care of interrupting
-	     the inferior.  Note that this case is working starting with
-	     Windows XP.  For Windows 2000, Ctrl-C should be pressed in the
-	     inferior console.
-
-	   - The debugger and the program share the same console, in which
-	     case both debugger and inferior will receive the Ctrl-c event.
-	     In that case the ctrl_c handler will ignore the event, as the
-	     Ctrl-c event generated inside the inferior will trigger the
-	     expected debug event.
-
-	     FIXME: brobecker/2008-05-20: If the inferior receives the
-	     signal first and the delay until GDB receives that signal
-	     is sufficiently long, GDB can sometimes receive the SIGINT
-	     after we have unblocked the CTRL+C handler.  This would
-	     lead to the debugger stopping prematurely while handling
-	     the new-thread event that comes with the handling of the SIGINT
-	     inside the inferior, and then stop again immediately when
-	     the user tries to resume the execution in the inferior.
-	     This is a classic race that we should try to fix one day.  */
-      SetConsoleCtrlHandler (&ctrl_c_handler, TRUE);
       ptid_t result = get_windows_debug_event (pid, ourstatus, options);
-      SetConsoleCtrlHandler (&ctrl_c_handler, FALSE);
 
       if (result != null_ptid)
 	{
-- 
2.35.1


       reply	other threads:[~2022-11-28 19:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20221128191526.1426-1-ssbssa.ref@yahoo.de>
2022-11-28 19:15 ` Hannes Domani [this message]
2022-12-01 17:30   ` Tom Tromey
2022-12-01 19:06     ` Hannes Domani
2022-12-01 21:22       ` Tom Tromey
2022-12-02 16:12         ` Tom Tromey
2022-12-02 22:22           ` Tom Tromey
2022-12-02 22:51             ` Hannes Domani
2022-12-05 18:54               ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221128191526.1426-1-ssbssa@yahoo.de \
    --to=ssbssa@yahoo.de \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).