Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/5] Fix "set cwd ..." on Cygwin, part 2
Date: Fri, 22 May 2026 01:16:23 +0100	[thread overview]
Message-ID: <20260522001626.393908-3-pedro@palves.net> (raw)
In-Reply-To: <20260522001626.393908-1-pedro@palves.net>

Even after the previous patch, on both native and gdbserver Cygwin, we
get:

 (gdb) set cwd /cygdrive/d/cygwin-gdb/build-testsuite/outputs/gdb.base/exitsignal
 (gdb) start
 Temporary breakpoint 3 at 0x100401094: file /home/alves/rocm/gdb/src/gdb/testsuite/gdb.base/segv.c, line 26.
 Starting program: /cygdrive/d/cygwin-gdb/build-testsuite/outputs/gdb.base/exitsignal/exitsignal.exe
 ❌️ Error creating process /cygdrive/d/cygwin-gdb/build-testsuite/outputs/gdb.base/exitsignal/exitsignal.exe (error 6): The handle is invalid.
 (gdb)

On the native side, this is because in
windows_nat_target::create_inferior, we unconditionally convert
forward slashes to backward slashes:

  /cygdrive/d/cygwin-gdb/build-testsuite/outputs/gdb.base/exitsignal
  =>
  \cygdrive\d\cygwin-gdb\build-testsuite\outputs\gdb.base\exitsignal

and then cygwin_conv_path(CCP_POSIX_TO_WIN_W) does nothing on such
path, as the backward slashes make the path not look like a Unix-style
path.

CreateProcess then fails to CD into that directory, as that's not a
real Windows native path.

The fix is to not do the slashes replacement on Cygwin.

On the gdbserver side, we're just completely missing the
cygwin_conv_path logic.  This commit adds it.  The code isn't shared
with GDB because GDB uses wide chars, and gdbserver uses narrow char.

Change-Id: I004f2a562757a566423f6acb9aecfcc1a7f2f746
commit-id: 85aa8c22
---
 gdb/windows-nat.c      | 14 ++++++++------
 gdbserver/win32-low.cc | 30 +++++++++++++++++++++++++-----
 2 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 862568fa21e..a284438bd36 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -2881,10 +2881,17 @@ windows_nat_target::create_inferior (const char *exec_file,
   else
     {
       expanded_infcwd = gdb_tilde_expand (inferior_cwd);
+      inferior_cwd = expanded_infcwd.c_str ();
+#ifndef __CYGWIN__
       /* Mirror slashes on inferior's cwd.  */
       std::replace (expanded_infcwd.begin (), expanded_infcwd.end (),
 		    '/', '\\');
-      inferior_cwd = expanded_infcwd.c_str ();
+#else
+      if (cygwin_conv_path (CCP_POSIX_TO_WIN_W,
+			    inferior_cwd,
+			    infcwd, sizeof (infcwd)) < 0)
+	error (_("Error converting inferior cwd: %d"), errno);
+#endif
     }
 
   memset (&si, 0, sizeof (si));
@@ -2923,11 +2930,6 @@ windows_nat_target::create_inferior (const char *exec_file,
       flags |= DEBUG_PROCESS;
     }
 
-  if (inferior_cwd != NULL
-      && cygwin_conv_path (CCP_POSIX_TO_WIN_W, inferior_cwd,
-			   infcwd, sizeof (infcwd)) < 0)
-    error (_("Error converting inferior cwd: %d"), errno);
-
   args = (wchar_t *) alloca ((wcslen (toexec) + wcslen (cygallargs) + 2)
 			     * sizeof (wchar_t));
   wcscpy (args, toexec);
diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc
index 469ff32f070..6f1cf5ed025 100644
--- a/gdbserver/win32-low.cc
+++ b/gdbserver/win32-low.cc
@@ -448,9 +448,11 @@ static BOOL
 create_process (const char *program, char *args,
 		DWORD flags, PROCESS_INFORMATION *pi)
 {
-  const std::string &inferior_cwd = get_inferior_cwd ();
   BOOL ret;
   size_t argslen, proglen;
+#ifdef __CYGWIN__
+  char infcwd_buf[PATH_MAX];
+#endif
 
   proglen = strlen (program) + 1;
   argslen = strlen (args) + proglen;
@@ -458,6 +460,27 @@ create_process (const char *program, char *args,
   STARTUPINFOA si = { sizeof (STARTUPINFOA) };
   char *program_and_args = (char *) alloca (argslen + 1);
 
+  const char *inferior_cwd = get_inferior_cwd ().c_str ();
+  std::string expanded_infcwd;
+  if (*inferior_cwd == '\0')
+    inferior_cwd = nullptr;
+  else
+    {
+      expanded_infcwd = gdb_tilde_expand (inferior_cwd);
+      inferior_cwd = expanded_infcwd.c_str ();
+#ifndef __CYGWIN__
+      /* Mirror slashes on inferior's cwd.  */
+      std::replace (expanded_infcwd.begin (), expanded_infcwd.end (),
+		    '/', '\\');
+#else
+      if (cygwin_conv_path (CCP_POSIX_TO_WIN_A,
+			    inferior_cwd,
+			    infcwd_buf, sizeof (infcwd_buf)) < 0)
+	error (_("Error converting inferior cwd: %d"), errno);
+      inferior_cwd = infcwd_buf;
+#endif
+    }
+
   strcpy (program_and_args, program);
   strcat (program_and_args, " ");
   strcat (program_and_args, args);
@@ -465,10 +488,7 @@ create_process (const char *program, char *args,
 			program_and_args,  /* command line */
 			flags,             /* start flags */
 			NULL,              /* environment */
-			/* current directory */
-			(inferior_cwd.empty ()
-			 ? NULL
-			 : gdb_tilde_expand (inferior_cwd).c_str()),
+			inferior_cwd,      /* current directory */
 			get_client_state ().disable_randomization,
 			&si,               /* start info */
 			pi);               /* proc info */
-- 
2.53.0


  parent reply	other threads:[~2026-05-22  0:16 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-22  0:16 [PATCH 0/5] Fix a few Cygwin/MinGW problems Pedro Alves
2026-05-22  0:16 ` [PATCH 1/5] Fix "set cwd ..." on Cygwin, part 1 Pedro Alves
2026-05-22 13:54   ` Tom Tromey
2026-05-22  0:16 ` Pedro Alves [this message]
2026-05-22 14:37   ` [PATCH 2/5] Fix "set cwd ..." on Cygwin, part 2 Tom Tromey
2026-05-25 16:43     ` Pedro Alves
2026-05-22  0:16 ` [PATCH 3/5] Adjust gdb.base/exitsignal.exp for Cygwin Pedro Alves
2026-05-22 15:09   ` Tom Tromey
2026-05-22  0:16 ` [PATCH 4/5] Fix exit/signal code on Cygwin Pedro Alves
2026-05-22  7:15   ` Eli Zaretskii
2026-05-25 16:47     ` Pedro Alves
2026-05-22  0:16 ` [PATCH 5/5] Adjust gdb.python/py-events.exp for Cygwin/MinGW Pedro Alves
2026-05-22  7:18   ` Eli Zaretskii

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=20260522001626.393908-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