[PATCH] Fix program invocation by gdbserver on MS-Windows

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Fix program invocation by gdbserver on MS-Windows

Eli Zaretskii
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  <[hidden email]>
+
+ * 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  <[hidden email]>
 
  * 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
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Fix program invocation by gdbserver on MS-Windows

Kevin Buettner
On Tue, 24 Dec 2019 19:39:41 +0200
Eli Zaretskii <[hidden email]> 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  <[hidden email]>
> +
> + * 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

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Fix program invocation by gdbserver on MS-Windows

Eli Zaretskii
> Date: Thu, 13 Feb 2020 06:34:23 -0700
> From: Kevin Buettner <[hidden email]>
> Cc: Eli Zaretskii <[hidden email]>
>
> On Tue, 24 Dec 2019 19:39:41 +0200
> Eli Zaretskii <[hidden email]> 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  <[hidden email]>
> > +
> > + * 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.