public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/2] Drop special way of getting inferior context after a Cygwin signal
Date: Tue, 20 Feb 2024 16:56:04 +0000	[thread overview]
Message-ID: <20240220165605.563516-3-pedro@palves.net> (raw)
In-Reply-To: <20240220165605.563516-1-pedro@palves.net>

From: Jon Turney <jon.turney@dronecode.org.uk>

Simplify Cygwin signal handling by dropping the special way of getting
inferior context after a Cygwin signal.

I think the reason this existed was because previously we were not
able to unwind through the alternate stack used by _sigfe frames, so
without the hint of the "user" code IP, the backtrace from a signal
was unusable.

Now we can unwind through _sigfe frames, drop all this complexity.

(Restoring this specially obtained context to the inferior (as the
code currently does) skips over the actual signal delivery and
handling.  Cygwin has carried for a long time a patch which clears the
ContextFlags in the signal context, so we never attempt to restore it
to the inferior, but that interfers with gdb's ability to modify that
context e.g. if it decides it wants to turn on FLAG_TRACE_BIT.)

Change-Id: I214edd5a99fd17c1a31ad18138d4a6cc420225e3
---
 gdb/windows-nat.c | 52 ++++++++++++++---------------------------------
 1 file changed, 15 insertions(+), 37 deletions(-)

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 7f3044fc61d..a90388922e2 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -98,10 +98,6 @@ struct windows_per_inferior : public windows_process_info
   void handle_unload_dll () override;
   bool handle_access_violation (const EXCEPTION_RECORD *rec) override;
 
-
-  int have_saved_context = 0;	/* True if we've saved context from a
-				   cygwin signal.  */
-
   uintptr_t dr[8] {};
 
   int windows_initialization_done = 0;
@@ -140,9 +136,6 @@ struct windows_per_inferior : public windows_process_info
   std::vector<windows_solib> solibs;
 
 #ifdef __CYGWIN__
-  CONTEXT saved_context {};	/* Contains the saved context from a
-				   cygwin signal.  */
-
   /* The starting and ending address of the cygwin1.dll text segment.  */
   CORE_ADDR cygwin_load_start = 0;
   CORE_ADDR cygwin_load_end = 0;
@@ -718,19 +711,6 @@ windows_nat_target::fetch_registers (struct regcache *regcache, int r)
 
   if (th->reload_context)
     {
-#ifdef __CYGWIN__
-      if (windows_process.have_saved_context)
-	{
-	  /* Lie about where the program actually is stopped since
-	     cygwin has informed us that we should consider the signal
-	     to have occurred at another location which is stored in
-	     "saved_context.  */
-	  memcpy (&th->context, &windows_process.saved_context,
-		  __COPY_CONTEXT_SIZE);
-	  windows_process.have_saved_context = 0;
-	}
-      else
-#endif
 #ifdef __x86_64__
       if (windows_process.wow64_process)
 	{
@@ -1048,33 +1028,32 @@ windows_per_inferior::handle_output_debug_string
 #ifdef __CYGWIN__
   else
     {
-      /* Got a cygwin signal marker.  A cygwin signal is followed by
-	 the signal number itself and then optionally followed by the
-	 thread id and address to saved context within the DLL.  If
-	 these are supplied, then the given thread is assumed to have
-	 issued the signal and the context from the thread is assumed
-	 to be stored at the given address in the inferior.  Tell gdb
-	 to treat this like a real signal.  */
+      /* Got a cygwin signal marker.  A cygwin signal marker is
+	 followed by the signal number itself, and (since Cygwin 1.7)
+	 the thread id, and the address of a saved context in the
+	 inferior (That context has an IP which is the return address
+	 in "user" code of the cygwin internal signal handling code,
+	 but is not otherwise usable).
+
+	 Tell gdb to treat this like the given thread issued a real
+	 signal.  */
       char *p;
       int sig = strtol (s.get () + sizeof (_CYGWIN_SIGNAL_STRING) - 1, &p, 0);
       gdb_signal gotasig = gdb_signal_from_host (sig);
+      LPCVOID x = 0;
 
       if (gotasig)
 	{
-	  LPCVOID x;
-	  SIZE_T n;
-
 	  ourstatus->set_stopped (gotasig);
 	  retval = strtoul (p, &p, 0);
 	  if (!retval)
 	    retval = current_event.dwThreadId;
-	  else if ((x = (LPCVOID) (uintptr_t) strtoull (p, NULL, 0))
-		   && ReadProcessMemory (handle, x,
-					 &saved_context,
-					 __COPY_CONTEXT_SIZE, &n)
-		   && n == __COPY_CONTEXT_SIZE)
-	    have_saved_context = 1;
+	  else
+	    x = (LPCVOID) (uintptr_t) strtoull (p, NULL, 0);
 	}
+
+      DEBUG_EVENTS ("gdb: cygwin signal %d, thread 0x%x, CONTEXT @ %p",
+		    gotasig, retval, x);
     }
 #endif
 
@@ -1607,7 +1586,6 @@ windows_nat_target::get_windows_debug_event
 
   event_code = windows_process.current_event.dwDebugEventCode;
   ourstatus->set_spurious ();
-  windows_process.have_saved_context = 0;
 
   switch (event_code)
     {
-- 
2.43.2


  parent reply	other threads:[~2024-02-20 16:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-20 16:56 [PATCH 0/2] Rework Cygwin signal handling Pedro Alves
2024-02-20 16:56 ` [PATCH 1/2] Teach gdb how to unwind cygwin _sigbe and sigdelayed frames Pedro Alves
2024-02-20 16:56 ` Pedro Alves [this message]
2024-02-21 13:25 ` [PATCH 0/2] Rework Cygwin signal handling Jon Turney
2024-02-23 16:24   ` Pedro Alves
2024-02-21 21:14 ` Tom Tromey
2024-02-23 16:25   ` Pedro Alves

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=20240220165605.563516-3-pedro@palves.net \
    --to=pedro@palves.net \
    --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).