public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix program invocation by gdbserver on MS-Windows
@ 2019-12-24 17:39 Eli Zaretskii
  2020-02-13 13:34 ` Kevin Buettner
  0 siblings, 1 reply; 3+ messages in thread
From: Eli Zaretskii @ 2019-12-24 17:39 UTC (permalink / raw)
  To: gdb-patches

gdbserver doesn't construct the command-line correctly when it invokes
programs on MS-Windows.  For example, if you invoke

      gdbserver :9999 etags.exe --help

and then connect from GDB and run the inferior, you will see:

  Remote debugging from host 127.0.0.1, port 3546
  --help: no input files specified.
	  Try '--help --help' for a complete list of options.

  Child exited with status 1

Which means that (a) the program exited abnormally; and (b) it
received "--help" in argv[0].

This is because the way we construct the command line in win32-low.c
is incorrect: we should prepend the program's name to the arguments.

The patch below does that.  OK to commit it?

diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog
index 028d1e9..29de8b6 100644
--- a/gdb/gdbserver/ChangeLog
+++ b/gdb/gdbserver/ChangeLog
@@ -1,3 +1,10 @@
+2019-12-24  Eli Zaretskii  <eliz@gnu.org>
+
+	* win32-low.c (create_process): Prepend PROGRAM to ARGS when
+	preparing the command line for CreateProcess.
+	(win32_create_inferior): Reflect the program name in debugging
+	output that shows the process and its command line.
+
 2019-12-19  Christian Biesinger  <cbiesinger@google.com>
 
 	* configure: Regenerate.
diff --git a/gdb/gdbserver/win32-low.c b/gdb/gdbserver/win32-low.c
index 449ed5f..9aaed84 100644
--- a/gdb/gdbserver/win32-low.c
+++ b/gdb/gdbserver/win32-low.c
@@ -557,21 +557,25 @@ create_process (const char *program, char *args,
 {
   const char *inferior_cwd = get_inferior_cwd ();
   BOOL ret;
+  size_t argslen, proglen;
+
+  proglen = strlen (program) + 1;
+  argslen = strlen (args) + proglen;
 
 #ifdef _WIN32_WCE
   wchar_t *p, *wprogram, *wargs, *wcwd = NULL;
-  size_t argslen;
 
-  wprogram = alloca ((strlen (program) + 1) * sizeof (wchar_t));
-  mbstowcs (wprogram, program, strlen (program) + 1);
+  wprogram = (wchar_t *) alloca (proglen * sizeof (wchar_t));
+  mbstowcs (wprogram, program, proglen);
 
   for (p = wprogram; *p; ++p)
     if (L'/' == *p)
       *p = L'\\';
 
-  argslen = strlen (args);
   wargs = alloca ((argslen + 1) * sizeof (wchar_t));
-  mbstowcs (wargs, args, argslen + 1);
+  wcscpy (wargs, wprogram);
+  wcscat (wargs, L" ");
+  mbstowcs (wargs + proglen, args, argslen + 1 - proglen);
 
   if (inferior_cwd != NULL)
     {
@@ -599,20 +603,24 @@ Could not convert the expanded inferior cwd to wide-char."));
 			pi);      /* proc info */
 #else
   STARTUPINFOA si = { sizeof (STARTUPINFOA) };
-
-  ret = CreateProcessA (program,  /* image name */
-			args,     /* command line */
-			NULL,     /* security */
-			NULL,     /* thread */
-			TRUE,     /* inherit handles */
-			flags,    /* start flags */
-			NULL,     /* environment */
+  char *program_and_args = (char *) alloca (argslen + 1);
+
+  strcpy (program_and_args, program);
+  strcat (program_and_args, " ");
+  strcat (program_and_args, args);
+  ret = CreateProcessA (program,           /* image name */
+			program_and_args,  /* command line */
+			NULL,              /* security */
+			NULL,              /* thread */
+			TRUE,              /* inherit handles */
+			flags,             /* start flags */
+			NULL,              /* environment */
 			/* current directory */
 			(inferior_cwd == NULL
 			 ? NULL
 			 : gdb_tilde_expand (inferior_cwd).c_str()),
-			&si,      /* start info */
-			pi);      /* proc info */
+			&si,               /* start info */
+			pi);               /* proc info */
 #endif
 
   return ret;
@@ -663,7 +671,7 @@ win32_create_inferior (const char *program,
   program = real_path;
 #endif
 
-  OUTMSG2 (("Command line is \"%s\"\n", args));
+  OUTMSG2 (("Command line is \"%s %s\"\n", program, args));
 
 #ifdef CREATE_NEW_PROCESS_GROUP
   flags |= CREATE_NEW_PROCESS_GROUP;
@@ -686,12 +694,12 @@ win32_create_inferior (const char *program,
 
   if (!ret)
     {
-      error ("Error creating process \"%s%s\", (error %d): %s\n",
+      error ("Error creating process \"%s %s\", (error %d): %s\n",
 	     program, args, (int) err, strwinerror (err));
     }
   else
     {
-      OUTMSG2 (("Process created: %s\n", (char *) args));
+      OUTMSG2 (("Process created: %s %s\n", program, (char *) args));
     }
 
 #ifndef _WIN32_WCE

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Fix program invocation by gdbserver on MS-Windows
  2019-12-24 17:39 [PATCH] Fix program invocation by gdbserver on MS-Windows Eli Zaretskii
@ 2020-02-13 13:34 ` Kevin Buettner
  2020-02-14  9:56   ` Eli Zaretskii
  0 siblings, 1 reply; 3+ messages in thread
From: Kevin Buettner @ 2020-02-13 13:34 UTC (permalink / raw)
  To: gdb-patches; +Cc: Eli Zaretskii

On Tue, 24 Dec 2019 19:39:41 +0200
Eli Zaretskii <eliz@gnu.org> wrote:

> gdbserver doesn't construct the command-line correctly when it invokes
> programs on MS-Windows.  For example, if you invoke
> 
>       gdbserver :9999 etags.exe --help
> 
> and then connect from GDB and run the inferior, you will see:
> 
>   Remote debugging from host 127.0.0.1, port 3546
>   --help: no input files specified.
> 	  Try '--help --help' for a complete list of options.
> 
>   Child exited with status 1
> 
> Which means that (a) the program exited abnormally; and (b) it
> received "--help" in argv[0].
> 
> This is because the way we construct the command line in win32-low.c
> is incorrect: we should prepend the program's name to the arguments.
> 
> The patch below does that.  OK to commit it?
> 
> diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog
> index 028d1e9..29de8b6 100644
> --- a/gdb/gdbserver/ChangeLog
> +++ b/gdb/gdbserver/ChangeLog
> @@ -1,3 +1,10 @@
> +2019-12-24  Eli Zaretskii  <eliz@gnu.org>
> +
> +	* win32-low.c (create_process): Prepend PROGRAM to ARGS when
> +	preparing the command line for CreateProcess.
> +	(win32_create_inferior): Reflect the program name in debugging
> +	output that shows the process and its command line.
> +

LGTM.

Kevin

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Fix program invocation by gdbserver on MS-Windows
  2020-02-13 13:34 ` Kevin Buettner
@ 2020-02-14  9:56   ` Eli Zaretskii
  0 siblings, 0 replies; 3+ messages in thread
From: Eli Zaretskii @ 2020-02-14  9:56 UTC (permalink / raw)
  To: Kevin Buettner; +Cc: gdb-patches

> Date: Thu, 13 Feb 2020 06:34:23 -0700
> From: Kevin Buettner <kevinb@redhat.com>
> Cc: Eli Zaretskii <eliz@gnu.org>
> 
> On Tue, 24 Dec 2019 19:39:41 +0200
> Eli Zaretskii <eliz@gnu.org> wrote:
> 
> > gdbserver doesn't construct the command-line correctly when it invokes
> > programs on MS-Windows.  For example, if you invoke
> > 
> >       gdbserver :9999 etags.exe --help
> > 
> > and then connect from GDB and run the inferior, you will see:
> > 
> >   Remote debugging from host 127.0.0.1, port 3546
> >   --help: no input files specified.
> > 	  Try '--help --help' for a complete list of options.
> > 
> >   Child exited with status 1
> > 
> > Which means that (a) the program exited abnormally; and (b) it
> > received "--help" in argv[0].
> > 
> > This is because the way we construct the command line in win32-low.c
> > is incorrect: we should prepend the program's name to the arguments.
> > 
> > The patch below does that.  OK to commit it?
> > 
> > diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog
> > index 028d1e9..29de8b6 100644
> > --- a/gdb/gdbserver/ChangeLog
> > +++ b/gdb/gdbserver/ChangeLog
> > @@ -1,3 +1,10 @@
> > +2019-12-24  Eli Zaretskii  <eliz@gnu.org>
> > +
> > +	* win32-low.c (create_process): Prepend PROGRAM to ARGS when
> > +	preparing the command line for CreateProcess.
> > +	(win32_create_inferior): Reflect the program name in debugging
> > +	output that shows the process and its command line.
> > +
> 
> LGTM.

Thanks, pushed to the master branch.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-02-14  9:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-24 17:39 [PATCH] Fix program invocation by gdbserver on MS-Windows Eli Zaretskii
2020-02-13 13:34 ` Kevin Buettner
2020-02-14  9:56   ` Eli Zaretskii

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).