public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jon TURNEY <jturney@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: Remove waitloop argument from try_to_debug()
Date: Sun, 30 Aug 2020 15:27:56 +0000 (GMT)	[thread overview]
Message-ID: <20200830152756.18FCE3857C4E@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=a30cd7a5b95675adf31b6092235b6c1f42c59520

commit a30cd7a5b95675adf31b6092235b6c1f42c59520
Author: Jon Turney <jon.turney@dronecode.org.uk>
Date:   Thu Aug 27 16:12:47 2020 +0100

    Cygwin: Remove waitloop argument from try_to_debug()
    
    Currently, when using CYGWIN's error_start facility, the faulting
    process isn't stopped while the error_start process is started when the
    fault is caused by an exception. (it even seems possible in theory that
    the faulting process could have exited before the error_start process
    attaches).
    
    This leads to e.g. the core dump written by CYGWIN='error_start=dumper'
    in response to an exception being non-deterministic.
    
    Remove the waitloop argument from try_to_debug(), only used in the
    exception case, so the faulting process busy-waits until the error_start
    process attaches.
    
    Code archaeology to determine why the code is this way didn't really
    turn up any answers, but this seems a low-risk change, as this only
    changes the behaviour when:
    
     - a debugger isn't already attached
     - an error_start is specified in CYGWIN env var
     - an exception has occurred which will be translated to a signal
    
    If error_start invokes something which doesn't attach using
    DebugActiveProcess(), we will spin indefinitely, but that will also
    currently occur for any of the existing other uses of try_to_debug(),
    which default to waitloop=TRUE.

Diff:
---
 winsup/cygwin/exceptions.cc | 14 ++++++++------
 winsup/cygwin/winsup.h      |  2 +-
 2 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/winsup/cygwin/exceptions.cc b/winsup/cygwin/exceptions.cc
index bb7704f94..c98b92d30 100644
--- a/winsup/cygwin/exceptions.cc
+++ b/winsup/cygwin/exceptions.cc
@@ -461,10 +461,8 @@ cygwin_stackdump ()
   exc.dumpstack ();
 }
 
-#define TIME_TO_WAIT_FOR_DEBUGGER 10000
-
 extern "C" int
-try_to_debug (bool waitloop)
+try_to_debug ()
 {
   if (!debugger_command)
     return 0;
@@ -532,13 +530,17 @@ try_to_debug (bool waitloop)
 			&si,
 			&pi);
 
+  /* We want to stop here and wait until the error_start process attaches.  But
+     we can't wait here for the error_start process to exit, as if it's a
+     debugger, it might want to continue this thread.  So we busy wait until a
+     debugger attaches, which stops this process, after which it can decide if
+     we continue or not. */
+
   *dbg_end = L'\0';
   if (!dbg)
     system_printf ("Failed to start debugger, %E");
   else
     {
-      if (!waitloop)
-	return dbg;
       SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_IDLE);
       while (!being_debugged ())
 	Sleep (1);
@@ -812,7 +814,7 @@ exception::handle (EXCEPTION_RECORD *e, exception_list *frame, CONTEXT *in,
   if (exit_state >= ES_SIGNAL_EXIT
       && (NTSTATUS) e->ExceptionCode != STATUS_CONTROL_C_EXIT)
     api_fatal ("Exception during process exit");
-  else if (!try_to_debug (0))
+  else if (!try_to_debug ())
     rtl_unwind (frame, e);
   else
     {
diff --git a/winsup/cygwin/winsup.h b/winsup/cygwin/winsup.h
index 79844cb87..0ffd8c5af 100644
--- a/winsup/cygwin/winsup.h
+++ b/winsup/cygwin/winsup.h
@@ -190,7 +190,7 @@ void close_all_files (bool = false);
 
 /* debug_on_trap support. see exceptions.cc:try_to_debug() */
 extern "C" void error_start_init (const char*);
-extern "C" int try_to_debug (bool waitloop = 1);
+extern "C" int try_to_debug ();
 
 void ld_preload ();
 void fixup_hooks_after_fork ();


                 reply	other threads:[~2020-08-30 15:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20200830152756.18FCE3857C4E@sourceware.org \
    --to=jturney@sourceware.org \
    --cc=cygwin-cvs@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).