gdb/Cygwin: Fix attach pid error message

Message ID 20240429145500.3956772-1-pedro@palves.net
State New
Headers
Series gdb/Cygwin: Fix attach pid error message |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 warning Patch is already merged
linaro-tcwg-bot/tcwg_gdb_build--master-arm warning Patch is already merged

Commit Message

Pedro Alves April 29, 2024, 2:55 p.m. UTC
  On Cygwin, with "attach PID":

 - GDB first tries to interpret PID as a Windows native PID, and tries
   to attach to that.

 - if the attach fails, GDB then tries to interpret the PID as a
   Cygwin PID, and attach to that.

If converting the user-provided PID from a Cygwin PID to a Windows PID
fails, you get this:

 (gdb) attach 12345
 Can't attach to process 0 (error 2: The system cannot find the file specified.)

Note "process 0".

With the fix in this commit, we'll now get:

 (gdb) attach 12345
 Can't attach to process 12345 (error 2: The system cannot find the file specified.)

I noticed this while looking at gdb.log after running
gdb.base/attach.exp on Cygwin.

Change-Id: I05b9dc1f3a634a822ea49bb5c61719f5e62c8514
---
 gdb/windows-nat.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)


base-commit: 2fb3ca4e88a8266427475596de8c667c9862e953
  

Comments

Luis Machado April 29, 2024, 3 p.m. UTC | #1
On 4/29/24 15:55, Pedro Alves wrote:
> On Cygwin, with "attach PID":
> 
>  - GDB first tries to interpret PID as a Windows native PID, and tries
>    to attach to that.
> 
>  - if the attach fails, GDB then tries to interpret the PID as a
>    Cygwin PID, and attach to that.
> 
> If converting the user-provided PID from a Cygwin PID to a Windows PID
> fails, you get this:
> 
>  (gdb) attach 12345
>  Can't attach to process 0 (error 2: The system cannot find the file specified.)
> 
> Note "process 0".
> 
> With the fix in this commit, we'll now get:
> 
>  (gdb) attach 12345
>  Can't attach to process 12345 (error 2: The system cannot find the file specified.)
> 
> I noticed this while looking at gdb.log after running
> gdb.base/attach.exp on Cygwin.
> 
> Change-Id: I05b9dc1f3a634a822ea49bb5c61719f5e62c8514
> ---
>  gdb/windows-nat.c | 17 +++++++++++++----
>  1 file changed, 13 insertions(+), 4 deletions(-)
> 
> diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
> index 3b3239ab938..70f955d9797 100644
> --- a/gdb/windows-nat.c
> +++ b/gdb/windows-nat.c
> @@ -2048,11 +2048,20 @@ windows_nat_target::attach (const char *args, int from_tty)
>  #ifdef __CYGWIN__
>        if (!ok)
>  	{
> -	  /* Try fall back to Cygwin pid.  */
> -	  pid = cygwin_internal (CW_CYGWIN_PID_TO_WINPID, pid);
> +	  /* Maybe PID was a Cygwin PID.  Try the corresponding native
> +	     Windows PID.  */
> +	  DWORD winpid = cygwin_internal (CW_CYGWIN_PID_TO_WINPID, pid);
>  
> -	  if (pid > 0)
> -	    ok = DebugActiveProcess (pid);
> +	  if (winpid != 0)
> +	    {
> +	      /* It was indeed a Cygwin PID.  Fully switch to the
> +		 Windows PID from here on.  We don't do this
> +		 unconditionally to avoid ending up with PID=0 in the
> +		 error message below.  */
> +	      pid = winpid;
> +
> +	      ok = DebugActiveProcess (winpid);
> +	    }
>  	}
>  #endif
>  
> 
> base-commit: 2fb3ca4e88a8266427475596de8c667c9862e953

LGTM.

Approved-By:  Luis Machado <luis.machado@arm.com>
  

Patch

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 3b3239ab938..70f955d9797 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -2048,11 +2048,20 @@  windows_nat_target::attach (const char *args, int from_tty)
 #ifdef __CYGWIN__
       if (!ok)
 	{
-	  /* Try fall back to Cygwin pid.  */
-	  pid = cygwin_internal (CW_CYGWIN_PID_TO_WINPID, pid);
+	  /* Maybe PID was a Cygwin PID.  Try the corresponding native
+	     Windows PID.  */
+	  DWORD winpid = cygwin_internal (CW_CYGWIN_PID_TO_WINPID, pid);
 
-	  if (pid > 0)
-	    ok = DebugActiveProcess (pid);
+	  if (winpid != 0)
+	    {
+	      /* It was indeed a Cygwin PID.  Fully switch to the
+		 Windows PID from here on.  We don't do this
+		 unconditionally to avoid ending up with PID=0 in the
+		 error message below.  */
+	      pid = winpid;
+
+	      ok = DebugActiveProcess (winpid);
+	    }
 	}
 #endif